package com.corn.turorial.spring.controller;

import com.corn.turorial.spring.config.PersonProp;
import io.micrometer.core.annotation.Timed;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Properties;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2021/2/4 15:08
 */

@RestController
@Timed
public class TestController {

    @Autowired
    private MeterRegistry meterRegistry;

    YamlPropertySourceLoader yamlPropertySourceLoader = new YamlPropertySourceLoader();


    @Timed(extraTags = "test.timed", longTask = true)
    @GetMapping("/simple")
    public String test() {
        return "test";
    }

    @GetMapping("/testMeter")
    public String testMeter() {
        Counter counter = meterRegistry.counter("myCounter");
        counter.increment();
        return "testMeter";
    }

    @GetMapping("/yaml")
    public Object testYaml() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("application.yml");
        List<PropertySource<?>> person = yamlPropertySourceLoader.load("person", classPathResource);
        return person;
    }
}
